#include "GraphicRender.h"


GraphicRender::GraphicRender(HWND hwnd, int width, int height)
{
	this->_hWnd = hwnd;
	this->_width = width;
	this->_height = height;
	this->_d3d = NULL;
	this->_d3ddev = NULL;
	this->_sprite = NULL;
}

bool GraphicRender::InitD3D()
{
	//tao mot doi tuong Direct3D
	this->_d3d = Direct3DCreate9(D3D_SDK_VERSION);
	if (!this->_d3d)
		return false;
	//tao thiet bi ve
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.BackBufferCount = 1;
	d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
	d3dpp.BackBufferWidth = this->_width;
	d3dpp.BackBufferHeight = this->_height;
	d3dpp.hDeviceWindow = this->_hWnd;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.Windowed = true;
	HRESULT hr = this->_d3d->CreateDevice(D3DADAPTER_DEFAULT,//chon thiet bi ve mac dinh
		D3DDEVTYPE_HAL,//su dung ho tro cua phan cung
		this->_hWnd,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING,
		&d3dpp,
		&this->_d3ddev);
	if (FAILED(hr))
	{
		return false;
	}
	hr = D3DXCreateSprite(this->_d3ddev, &this->_sprite);
	if (FAILED(hr))
		return false;
	return true;
}

GraphicRender::~GraphicRender(void)
{
}

void GraphicRender::Begin()
{
	this->_d3ddev->Clear(1, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255, 255, 255), 1, NULL);
	this->_d3ddev->BeginScene();
	this->_sprite->Begin(D3DXSPRITE_ALPHABLEND);
}
void GraphicRender::End()
{
	this->_sprite->End();
	this->_d3ddev->EndScene();
	this->_d3ddev->Present(NULL, NULL, NULL, NULL);
}
void GraphicRender::DrawTexture(LPDIRECT3DTEXTURE9 texture, D3DXVECTOR2 postion, D3DCOLOR color)
{
	this->_sprite->Draw(texture, NULL, NULL, &D3DXVECTOR3(postion.x, postion.y, 0), color);
}


void GraphicRender::DrawTexture(LPDIRECT3DTEXTURE9 texture, RECT source, D3DXVECTOR2 postion, D3DCOLOR color)
{
	this->_sprite->Draw(texture, &source, NULL, &D3DXVECTOR3(postion.x, postion.y, 0), color);
}

void GraphicRender::DrawSurface()
{
}


LPDIRECT3DDEVICE9 GraphicRender::GetDevice()
{
	return this->_d3ddev;
}


void GraphicRender::DrawString(char* str)
{

}
